package com.ttdevs.es;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.os.Bundle;
import android.preference.Preference;
import android.preference.Preference.OnPreferenceClickListener;
import android.preference.PreferenceFragment;
import android.preference.SwitchPreference;

import com.ttdevs.util.SharePreferenceUtil;

public class PreferenceActivity extends Activity {
	private static SharePreferenceUtil spUtil;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_preference);

		spUtil = new SharePreferenceUtil(getApplicationContext());

		getFragmentManager().beginTransaction().replace(R.id.llPref, new PrefsFragement()).commit();
	}

	public static class PrefsFragement extends PreferenceFragment implements OnPreferenceClickListener {

		private final static String right_grant = "right_grant";
		private final static String alarm_vibrator = "alarm_vibrator";
		private final static String alarm_ringing = "alarm_ringing";

		@Override
		public void onCreate(Bundle savedInstanceState) {
			super.onCreate(savedInstanceState);

			addPreferencesFromResource(R.xml.preferences);

			findPreference(right_grant).setOnPreferenceClickListener(this);
			findPreference(alarm_vibrator).setOnPreferenceClickListener(this);
			findPreference(alarm_ringing).setOnPreferenceClickListener(this);
		}

		@Override
		public boolean onPreferenceClick(Preference pref) {
			System.out.println(pref.getKey());
			if (right_grant.equals(pref.getKey())) {
				Intent intent = new Intent("/");
				ComponentName cm = new ComponentName("com.android.settings",
						"com.android.settings.applications.AppsCheckReadPermission");
				intent.setComponent(cm);
				intent.setAction("android.intent.action.VIEW");
				startActivity(intent);
			}
			if (alarm_vibrator.equals(pref.getKey())) {
				SwitchPreference spf = (SwitchPreference) pref;
				spUtil.putVibrate(spf.isChecked());
			}
			if(alarm_ringing.equals(pref.getKey())){
				SwitchPreference spf = (SwitchPreference) pref;
				spUtil.putRing(spf.isChecked());
			}
			return false;
		}
	}
}
